function output(message) {
  var element = $("<div>" + " " + message + "</div>");
  $("#console").prepend(element);
}
var data = {};
function isExist(data) {
  // 查询是否有这个房间
  return new Promise((resolve, reject) => {

    
    $.post("/api/room/isExist", data, function (res) {
      if (res.code != 0) {
        output(
          '<span class="disconnect-msg"><font color="blue">' +
            getNowTime() +
            "&nbsp;</font>" +
            res.message +
            "</span>",
        );
      }
      resolve(res);
    });
  });
}

async function createRoom(data) {
  var param = data;
  // 创建房间
  return new Promise((resolve, reject) => {
    $.post("/api/room/createRoom", data, function (res) {
      if (res.code != 0) {
        output(
          '<span class="disconnect-msg"><font color="blue">' +
            getNowTime() +
            "&nbsp;</font>" +
            res.message +
            "</span>",
        );
      } else {
        output(
          '<span class="disconnect-msg"><font color="blue">' +
            getNowTime() +
            "&nbsp;</font> 创建房间成功" +
            "</span>",
        );
      }

      resolve(res);
    });
  });
}

// 房间是存在的情况 用户存不存在房间
async function userJoinByExistRoom(data) {
  var param = {};
  param.roomId = data["roomId"];
  param.userId = data["userId"];
  return new Promise((resolve, reject) => {
    $.post("/api/room/userJoinByExistRoom", param, function (res) {
      console.log(res);
      resolve(res);
    });
  });
}
async function ready(data) {
  var param = {};
  param.roomId = data["roomId"];
  param.userId = data["userId"];
  return new Promise((resolve, reject) => {
    $.post("/api/room/ready", param, function (res) {
      console.log(res);
      resolve(res);
    });
  });
}
async function begin(data) {
  var param = {};
  param.roomId = data["roomId"];
  param.userId = data["userId"];
  return new Promise((resolve, reject) => {
    $.post("/api/room/begin", param, function (res) {
      console.log(res);
      resolve(res);
    });
  });
}
async function joinRoom(data) {
  var param = {};
  param.roomId = data["roomId"];
  param.userId = data["userId"];
  return new Promise((resolve, reject) => {
    $.post("/api/room/joinRoom", param, function (res) {
      console.log(res);
      roomId =data["roomId"]
      resolve(res);
    });
  });
}

function exitRoom(data) {
  var param = {};
  param.roomId = data["roomId"];
  param.userId = data["userId"];
  param.type = "-1";
  return new Promise((resolve, reject) => {
    $.post("/api/room/exitRoom", param, function (res) {
      if (res.code != 0) {
        output(
          '<span class="disconnect-msg"><font color="blue">' +
            getNowTime() +
            "&nbsp;</font>" +
            res.message +
            "</span>",
        );
      }
      resolve(res);
    });
  });
}

function post({url,body}){
  return new Promise((resolve,reject)=>{
    $.ajax({
      url: url, //请求的url地址
      dataType: "json", //返回格式为json
      async: true, //请求是否异步，默认为异步true，这也是ajax重要特性
      data:body, //参数值
      type: "POST", //请求方式
      beforeSend: function () {
        //请求前的处理
      },
      success: function (req) {
        //请求成功时处理
        resolve(req);
      },
      complete: function () {
        //请求完成的处理
      },
      error: function (r) {
        //请求出错处理
        reject(r)
      },
    });
  }) 
  
}

chupai =async (chupai) =>{
  console.log("出牌");
 
  var data = {
    userId: nickName,
    roomId:roomId,
    pai:getPointList([chupai])[0]
  };
  const sendInfo = {
    method: "POST",
    url: "/api/paiMsg/chupai",
    body: data,
    json: true,
  };
  let posting = await Promise.resolve(await post(sendInfo));
  console.log("posting:",JSON.stringify(posting.data));
  return posting.data;

}


 hu=async({userId,roomId},i=2)=>{
  console.log("糊了");
 
  var data = {
    userId: userId,
    roomId:roomId,
  };
  const sendInfo = {
    method: "POST",
    url: "/api/paiMsg/hu",
    body: data,
    json: true,
  };
  try {
    let posting = await Promise.resolve(await post(sendInfo));
    // console.log("posting:",JSON.stringify(posting.data));
    
  } catch (error) {
    
  }
 

}
 guo=async ({userId,roomId},i=2)=>{
  console.log("过了");
 
  var data = {
    userId: userId,
    roomId:roomId,
  };
  const sendInfo = {
    method: "POST",
    url: "/api/paiMsg/guo",
    body: data,
    json: true,
  };
  try {
    let posting = await Promise.resolve(await post(sendInfo));
    // console.log("posting:",JSON.stringify(posting.data));
   
  } catch (error) {
    
  }

}

peng=async ({userId,roomId},i=2)=>{
  console.log("碰了");
 
  var data = {
    userId: userId,
    roomId:roomId,
  };
  const sendInfo = {
    method: "POST",
    url: "/api/paiMsg/peng",
    body: data,
    json: true,
  };
  try {
    let posting = await Promise.resolve(await post(sendInfo));
    // console.log("posting:",JSON.stringify(posting.data));
    
  } catch (error) {
    
  }

}

gang=async ({userId,roomId,pai},i=2)=>{
  console.log("碰了");
 
  var data = {
    userId: userId,
    roomId:roomId,
    pai
  };
  const sendInfo = {
    method: "POST",
    url: "/api/paiMsg/gang",
    body: data,
    json: true,
  };
  try {
    let posting = await Promise.resolve(await post(sendInfo));
    // console.log("posting:",JSON.stringify(posting.data));
   
  } catch (error) {
    
  }
}
chi=async ({userId,roomId,index=0},i=2)=>{
  console.log("吃了");
 
  var data = {
    userId: userId,
    roomId:roomId,
    index
  };
  const sendInfo = {
    method: "POST",
    url: "/api/paiMsg/chi",
    body: data,
    json: true,
  };
  try {
    let posting = await Promise.resolve(await post(sendInfo));
    // console.log("posting:",JSON.stringify(posting.data));
   
  } catch (error) {
    
  }

}



 /**
     * 获取牌名
     * @param {String} indexList 
     */
getName = (indexList)=>{
    //去掉无效的牌名，比如十筒
    indexList = indexList.filter( (index) =>{
        return index != -1;
    })
    let displayNameList = ['1筒', '2筒', '3筒', '4筒', '5筒', '6筒', '7筒', '8筒', '9筒', '1条', '2条', '3条', '4条', '5条', '6条', '7条', '8条', '9条', '1万', '2万', '3万', '4万', '5万', '6万', '7万', '8万', '9万', '东', '南', '西', '北', '中', '发', '白'];
    
    let indexListName = indexList.map( (item) =>{
        return displayNameList[item];
    }) 
  
  return indexListName;
}

 getPointList=(otherNameList)=>{
  //根据牌名获取点数，然后根据点数创建牌
  let displayNameList = ['1筒', '2筒', '3筒', '4筒', '5筒', '6筒', '7筒', '8筒', '9筒', '1条', '2条', '3条', '4条', '5条', '6条', '7条', '8条', '9条', '1万', '2万', '3万', '4万', '5万', '6万', '7万', '8万', '9万', '东', '南', '西', '北', '中', '发', '白'];
  
  let indexList = otherNameList.map( (displayName) =>{
      return displayNameList.indexOf(displayName);
  })
  //去掉无效的牌名，比如十筒
  indexList = indexList.filter( (index) =>{
      return index != -1;
  })
  return indexList;
}

function sort(arr) {
  arr.sort(function (a,b) {
      return Number(a) - Number(b);
  })
  return arr;
}
function sortDesc(arr) {
  arr.sort(function (a,b) {
      return Number(b) - Number(a);
  })
  return arr;

}

var resp={
  game_holds: 57,
  mj_count: 62,
  game_chupai: 63,
  game_chupai_notify: 64,
  guo_notify: 65,
  game_mopai: 66,
  game_action: 67,
  peng_notify: 68,
  gang_notify: 69,
  hangang_notify: 70,
  game_ting: 71,
  game_over: 72,
  game_hupai: 73,
  chi_notify: 74,

  
}
